package com.nintendo.frame;

import com.nintendo.entity.pojo.Dish;
import com.nintendo.frame.edit.EditDishFrame;
import com.nintendo.mapper.DishDao;
import com.nintendo.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class DishListFrame extends JFrame {

    private JTable dishTable;
    private DefaultTableModel tableModel;
//    private DishDao dishDao;

    public DishListFrame() {
        setTitle("菜品列表");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        // 初始化表格模型
        tableModel = new DefaultTableModel(
                new Object[]{"ID", "名称", "编码", "单位", "价格", "状态"},
                0
        );

        // 创建表格
        dishTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(dishTable);
        add(scrollPane, BorderLayout.CENTER);

        // 初始化 DishDao
//        dishDao = new DishDaoImpl();
//        SqlSession sqlSession = MyBatisUtils.getSqlSession();
//        DishDao dishDao = sqlSession.getMapper(DishDao.class);
        // 加载数据
        loadData();

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        JButton addButton = new JButton("新增");
        JButton editButton = new JButton("修改");
        JButton deleteButton = new JButton("删除");

        buttonPanel.add(addButton);
        buttonPanel.add(editButton);
        buttonPanel.add(deleteButton);

        add(buttonPanel, BorderLayout.SOUTH);

        // 为按钮添加动作监听器
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addDish();
            }
        });

        editButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                editDish();
            }
        });

        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                deleteDish();
            }
        });

        setVisible(true);
    }

    private void loadData() {
        try (SqlSession session = MyBatisUtils.getSqlSession()) {
            DishDao dishDao = session.getMapper(DishDao.class);
            List<Dish> dishes = dishDao.selectAll();

            for (Dish dish : dishes) {
                tableModel.addRow(new Object[]{
                        dish.getId(),
                        dish.getName(),
                        dish.getCode(),
                        dish.getUnit(),
                        dish.getPrice(),
                        dish.getStatus()
                });
            }
        }
    }

    private void addDish() {
        // 打开新增菜品窗口
        new DishFrame().setVisible(true);
    }

    private void editDish() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        DishDao dishDao = sqlSession.getMapper(DishDao.class);
        int selectedRow = dishTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请选择要修改的菜品", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        int dishId = (int) tableModel.getValueAt(selectedRow, 0);
        Dish dish = dishDao.selectById(dishId);
        if (dish != null) {
            new EditDishFrame(this, dish).setVisible(true);
        } else {
            JOptionPane.showMessageDialog(this, "未找到选中的菜品", "错误", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void deleteDish() {
        int selectedRow = dishTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请选择要删除的菜品", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        int dishId = (int) tableModel.getValueAt(selectedRow, 0);
        int confirm = JOptionPane.showConfirmDialog(this, "确定要删除选中的菜品吗？", "确认删除", JOptionPane.YES_NO_OPTION);
        if (confirm == JOptionPane.YES_OPTION) {
            try (SqlSession session = MyBatisUtils.getSqlSession()) {
                DishDao dishDao = session.getMapper(DishDao.class);
                dishDao.deleteById(dishId);
                session.commit();
                tableModel.removeRow(selectedRow);
                JOptionPane.showMessageDialog(this, "删除成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "删除失败", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    public void refreshTable() {
        tableModel.setRowCount(0);
        loadData();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new DishListFrame();
        });
    }
}
